package com.example.coinhelper.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DetailsDBHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = UserDBHelper.DATABASE_NAME;//"coin.db";

    public static final String TABLE_DETAILS = "details_table";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_DATE = "date";
    public static final String COLUMN_OPERATION = "operation";
    public static final String COLUMN_MONEY = "money";
    public static final String COLUMN_ICON = "icon";
    public static final String COLUMN_OTHER = "other";
    public static final String CREATE_DETAILS_TABLE = "CREATE TABLE " + TABLE_DETAILS + "("
            + COLUMN_ID + " integer primary key autoincrement ,"
            + COLUMN_USERNAME + " TEXT,"
            + COLUMN_DATE + " TEXT,"
            + COLUMN_OPERATION + " TEXT,"
            + COLUMN_MONEY + " REAL,"
            + COLUMN_ICON + " INTEGER,"
            + COLUMN_OTHER + " TEXT,"
            + "FOREIGN KEY (" + COLUMN_USERNAME + ") REFERENCES user_table(username) ON DELETE CASCADE ON UPDATE CASCADE"
            + ")";
    public DetailsDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onConfigure(SQLiteDatabase db) {
        super.onConfigure(db);
        db.setForeignKeyConstraintsEnabled(true);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
//        String CREATE_DETAILS_TABLE = "CREATE TABLE " + TABLE_DETAILS + "("
//                + COLUMN_USERNAME + " TEXT,"
//                + COLUMN_DATE + " TEXT,"
//                + COLUMN_OPERATION + " TEXT,"
//                + COLUMN_MONEY + " REAL,"
//                + COLUMN_ICON + " INTEGER,"
//                + "FOREIGN KEY (" + COLUMN_USERNAME + ") REFERENCES user_table(username) ON DELETE CASCADE"
//                + ")";

        db.execSQL(CREATE_DETAILS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 升级数据库时执行的操作
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAILS);
        onCreate(db);
    }
    // 添加其他数据库操作方法，例如插入、查询等
}
